class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    public boolean isValidBST(TreeNode root) {
        return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }

    public boolean isValidBST(TreeNode node, long lower, long upper) {
        if (node == null) {
            return true;
        }
        if (node.val <= lower || node.val >= upper) {
            return false;
        }
        return isValidBST(node.left, lower, node.val) && isValidBST(node.right, node.val, upper);
    }

    public static void main(String[] args) {
        // 构造一个二叉树
        // 示例：构造一个有效的BST
        TreeNode root = new TreeNode(2);
        root.left = new TreeNode(1);
        root.right = new TreeNode(3);

        // 构造一个无效的BST
        // TreeNode root = new TreeNode(5);
        // root.left = new TreeNode(1);
        // root.right = new TreeNode(4);
        // root.right.left = new TreeNode(3);
        // root.right.right = new TreeNode(6);

        Solution solution = new Solution();
        boolean result = solution.isValidBST(root);
        System.out.println("Is the tree a valid BST? " + result);
    }
}